package auth

import (
	"context"
	"errors"
	"github.com/golang-jwt/jwt/v5"
	"webook/internal/service/sms"
)

type AuthSmsService struct {
	svc sms.Service
	key func(token *jwt.Token) (interface{}, error)
}

func NewAuthSmsService(svc sms.Service) *AuthSmsService {
	return &AuthSmsService{
		svc: svc,
	}
}
func (s *AuthSmsService) Send(ctx context.Context, phone string, templateToken string, templateParam string) error {
	// 1. 鉴权
	var claims TokenJwtClaims
	token, err := jwt.ParseWithClaims(templateToken, &claims, s.key)
	if err != nil {
		return err
	}
	if !token.Valid {
		return errors.New("invalid token")
	}
	// 2. 发送短信
	return s.svc.Send(ctx, phone, claims.templateCode, templateParam)
}

type TokenJwtClaims struct {
	jwt.RegisteredClaims
	templateCode string
}
